﻿using System;
using System.Data;
using Gencode.PrsCommon.App;

namespace Gencode.PrsCommon.Data.Plan
{
    /// <summary>
    /// Single Rowelement of the main grid
    /// </summary>
    public class Row
    {
        public enum ROWTYPE { Abteilung, Leer, Person, Taganzeige }

		Cell[] cells;

        public Row()
        {
        }

		public Row(int planid, int prsschemaid, int prsid)
		{
            InitData(planid, prsschemaid, prsid);
		}

        private void InitData(int planid, int prsschemaid, int prsid)
        {
            DataView dv = PlanData.GetPlanRow(planid, prsschemaid, prsid);

            int m = int.Parse(DateUtil.GetMonth(Von));
            int y = int.Parse(DateUtil.GetYear(Von));
            int days = DateTime.DaysInMonth(y, m);

            if (cells != null)
                cells = null;

            cells = new Cell[days];

            switch (RowType)
            {
                case ROWTYPE.Abteilung:
                    for (int d = 0; d < days; d++)
                    {
                        DateTime dat = new DateTime(y, m, d + 1);
                        cells[d] = new Cell
                        {
                            Datum = dat.ToShortDateString(),
                            Day = dat.DayOfWeek
                        };
                    }
                    break;

                case ROWTYPE.Leer:
                    for (int d = 0; d < days; d++)
                    {
                        DateTime dat = new DateTime(y, m, d + 1);
                        cells[d] = new Cell
                        {
                            Datum = dat.ToShortDateString(),
                            Day = dat.DayOfWeek
                        };
                    }
                    break;

                case ROWTYPE.Person:
                    RowType = ROWTYPE.Person;
                    int day = 0;
                    for (int d = 0; d < days; d++)
                    {
                        if (dv.Count > 0)
                        {
                            day = int.Parse(DateUtil.GetDay(DateUtil.ConvertSqlToDate(dv[0].Row["datum"].ToString())));
                        }

                        if ((d + 1) == day)
                        {
                            DateTime dat = new DateTime(y, m, d + 1);
                            cells[d] = new Cell
                            {
                                Datum = dat.ToShortDateString(),
                                Day = dat.DayOfWeek,
                                DienstID = int.Parse(dv[0].Row["dienstid"].ToString()),
                                DienstName = dv[0].Row["name"].ToString(),
                                ARGB = int.Parse(dv[0].Row["rgb"].ToString())
                            };
                            dv.Delete(0);
                        }
                        else
                        {
                            DateTime dat = new DateTime(y, m, d + 1);
                            cells[d] = new Cell
                            {
                                Datum = dat.ToShortDateString(),
                                Day = dat.DayOfWeek
                            };
                        }

                        day = 0;
                    }
                    break;

                case ROWTYPE.Taganzeige:
                    for (int d = 0; d < days; d++)
                    {
                        DateTime dat = new DateTime(y, m, d + 1);
                        cells[d] = new Cell
                        {
                            Datum = dat.ToShortDateString(),
                            Day = dat.DayOfWeek,
                            DienstName = DateUtil.DayInWeek(y, m, d + 1)
                        };
                    }
                    break;
            }
        }

        /// <summary>
        /// Initializes Row. PlanID and PrsSchemaID hav to be set before call.
        /// </summary>
        internal void InitData()
        {
            InitData(PlanID, PrsSchemaID, PrsID);
        }

		#region Properties

		/// <summary>
		/// Returns the GridCell element at the given Index
		/// </summary>
		/// <param name="column">Index of element</param>
		/// <returns></returns>
        public Cell this[int column]
		{
			get { return cells[column]; }
		}

        public Cell[] Cells
        {
            get
            {
                return cells;
            }
        }

        public string AbteilungName
        {
            get;
            set;
        }

        public int AbteilungID
        {
            get;
            set;
        }

        public int PrsSchemaID
        {
            get;
            set;
        }

        public string PersonName
        {
            get;
            set;
        }

        public string Von
        {
            get;
            set;
        }

        public string Bis
        {
            get;
            set;
        }

        public int ARGB
        {
            get;
            set;
        }

		public int Pensum
		{
            get;
            set;
		}

		public int PrsID
		{
            get;
            set;
		}

        public int PlanID
        {
            get;
            set;
        }

		public ROWTYPE RowType
		{
            get;
            set;
		}
		public double Haben
		{
            get;
            set;
		}
		public double Soll
		{
            get;
            set;
		}
		public double Diff
		{
			get { return Haben - (Soll + (SaldoVortrag * -1)); }
		}

		public double SaldoVortrag
		{
            get;
            set;
		}

		#endregion
    }
}
